#include "MonWidgetQuestionQCM.h"


MonWidgetQuestionQCM::MonWidgetQuestionQCM(const QString &question, const QStringList &reponses)
{
    this->question = question;
    this->reponses = reponses;
    this->cpt = 30;

    timer.setParent(this);
    timer.start(1000);


    layout = new QVBoxLayout(this);

    labelQuestion = new QLabel(question, this);
    layout->addWidget(labelQuestion);


    QStringList::Iterator it;
    for(it = this->reponses.begin(); it != this->reponses.end(); ++it)
    {
        QCheckBox* checkBox = new QCheckBox(*it, this);
        checkboxReponses.append(checkBox);
        layout->addWidget(checkBox);
    }

    boutonValider = new QPushButton("Valider", this);
    boutonValider->setIcon(QIcon(":/pic/envoyer"));
    layout->addWidget(boutonValider);
    layout->setMargin(15);

    connect(boutonValider, SIGNAL(clicked()), this, SLOT(onClick_boutonValider()));
    connect(&timer, SIGNAL(timeout()), this, SLOT(updateTemps()));
}

void MonWidgetQuestionQCM::updateTemps()
{
    --cpt;

    boutonValider->setText("Valider - Temps Restant : " + QString::number(cpt) + " sec");
    if(cpt > 15) // VERT
    {
        boutonValider->setStyleSheet("color : #04B91C");
    }
    else
    {
        if(cpt > 6) // ORANGE
        {
            boutonValider->setStyleSheet("color : #EC9626");
        }
        else
        {
            if(cpt > 0) // ROUGE
            {
                boutonValider->setStyleSheet("color : #C60003");
            }
            else // FIN DU TEMPS
            {
                timer.stop();
                onClick_boutonValider();
            }
        }
    }
}

void MonWidgetQuestionQCM::onClick_boutonValider()
{
    timer.stop();
    QStringList valueRetour;
    QList<QCheckBox*>::Iterator it = checkboxReponses.begin();
    while(it != checkboxReponses.end())
    {
        QCheckBox *checkB = (*it);
        if(checkB->isChecked())
        {
           valueRetour.append(reponses.front());
        }
        reponses.pop_front();
        ++it;
    }
    emit valider(valueRetour);
    setDisabled(true);
}
